import sys
import time
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QTextEdit, QLabel
from PyQt6.QtCore import Qt, QObject, QEvent, QThread, pyqtSignal

class EnterKeyFilter(QObject):
    def __init__(self, callback):
        super().__init__()
        self.callback = callback

    def eventFilter(self, watched, event):
        if event.type() == QEvent.Type.KeyPress and (event.key() == Qt.Key.Key_Return or event.key() == Qt.Key.Key_Enter):
            self.callback()
            return True
        return False

class ProcessThread(QThread):
    update_output = pyqtSignal(str)

    def __init__(self, input_data):
        super().__init__()
        self.input_data = input_data

    def run(self):
        lines = self.input_data.split('\n')
        for i in range(20):
            time.sleep(1)
            processed_lines = [line + f" + {i}" for line in lines if line.strip() != ""]
            self.update_output.emit("\n".join(processed_lines))

class NewWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("输入与输出")
        self.setFixedSize(800, 400)

        self.input_text = QTextEdit()
        self.output_text = QTextEdit()
        self.output_text.setReadOnly(True)

        self.process_button = QPushButton("处理数据")
        self.process_button.clicked.connect(self.start_processing)

        layout = QHBoxLayout()
        layout.addWidget(QLabel("输入:"))
        layout.addWidget(self.input_text)
        layout.addWidget(QLabel("输出:"))
        layout.addWidget(self.output_text)

        main_layout = QVBoxLayout()
        main_layout.addLayout(layout)
        main_layout.addWidget(self.process_button)

        container = QWidget()
        container.setLayout(main_layout)
        self.setCentralWidget(container)

        self.enter_key_filter = EnterKeyFilter(self.start_processing)
        self.input_text.installEventFilter(self.enter_key_filter)

    def start_processing(self):
        input_data = self.input_text.toPlainText()
        self.thread = ProcessThread(input_data)
        self.thread.update_output.connect(self.append_output)
        self.thread.start()

    def append_output(self, text):
        current_text = self.output_text.toPlainText()
        self.output_text.setPlainText(current_text + "\n" + text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = NewWindow()
    main_window.show()
    app.exec()
